package com.boarsoft.boar.demo.order;

import javax.servlet.http.HttpServlet;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.boarsoft.common.Util;

public class Main {
	private static ClassPathXmlApplicationContext ctx;

	public static void main(String[] args) throws LifecycleException {
		ctx = new ClassPathXmlApplicationContext("classpath:conf/context.xml");
		System.out.println("Startup ".concat(ctx.isRunning() ? "successfully." : "failed."));

		int hp = Config.HTTP_PORT;
		String port = System.getProperty("http.port");
		if (Util.strIsNotEmpty(port)) {
			hp = Integer.parseInt(port);
		}
		Tomcat tomcat = new Tomcat();
		tomcat.setPort(hp);
		tomcat.getHost().setAutoDeploy(false); // 创建上下文
		StandardContext context = new StandardContext();
		context.setPath("/order");
		context.addLifecycleListener(new Tomcat.FixContextListener());
		tomcat.getHost().addChild(context);
		tomcat.addServlet("/order", "order", ctx.getBean("orderServlet", HttpServlet.class)); //
		context.addServletMappingDecoded("/find", "order");
		context.addServletMappingDecoded("/place", "order");
		context.addServletMappingDecoded("/finish", "order");
		//
		Connector connector = tomcat.getConnector();
		Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
		protocol.setMaxConnections(2000);
		protocol.setMaxThreads(2000);
		protocol.setAcceptCount(2000);
		tomcat.start();
		System.out.println("Startup successfully.");
		tomcat.getServer().await();

		// Server server = new Server();// 创建jetty web容器
		// server.setStopAtShutdown(true);// 在退出程序是关闭服务
		// // 创建连接器，每个连接器都是由IP地址和端口号组成，连接到连接器的连接将会被jetty处理
		// Connector connector = new SelectChannelConnector();// 创建一个连接器
		// //connector.setHost("127.0.0.1");// ip地址
		// connector.setPort(8080);// 连接的端口号
		// server.addConnector(connector);// 添加连接
		// QueuedThreadPool threadPool = new QueuedThreadPool();
		// threadPool.setMaxThreads(3000);
		// server.setThreadPool(threadPool);
		// // 配置服务
		// WebAppContext context = new WebAppContext();// 创建服务上下文
		// context.setContextPath("/strutsDemo");// 访问服务路径 http://{ip}:{port}/
		// context.setConfigurationDiscovered(true);
		// String baseDir =
		// Thread.currentThread().getContextClassLoader().getResource("").getPath();
		// context.setDescriptor(baseDir + File.separator +
		// "/WEB-INF/web.xml");// 指明服务描述文件，就是web.xml
		// //
		// context.setDescriptor("/E:/workspace/strutsDemo/target/classes/\\/WEB-INF/web.xml");//
		// // 指明服务描述文件，就是web.xml
		// context.setResourceBase(System.getProperty("user.dir") +
		// "/src/main/webapp/");// 指定服务的资源根路径，配置文件的相对路径与服务根路径有关
		// server.setHandler(context);// 添加处理try {
		// server.start();// 开启服务
		// server.join();
	}
}
